﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ECCSystem.EntityClass;
using ECCSystem.DataAccess;
using ECCSystem.BizRule;
using ECC_EXT_DAO.Dao;
using ECC_EXT_Entity;

namespace ECCSystem
{
    public partial class frm_Ext_MailContentDetails : Form
    {
        public int type { get; set; }
        public EmailTempletEntity objEmailTempletEntity { get; set; }
        private MailContentDetailsDao _mailContentDetailsDao;
        public frm_Ext_MailContentDetails()
        {
            InitializeComponent();
            _mailContentDetailsDao = new MailContentDetailsDao();
        }

        private void frm_Ext_MailContentDetails_Load(object sender, EventArgs e)
        {
            
            this.Init();
        }

        private void Init()
        {
            var _mailContentDetailsEntity = new MailContentDetailsEntity();

            _mailContentDetailsEntity.EmailContentKeyID = this.objEmailTempletEntity.ContentKeyID;
            _mailContentDetailsEntity.Type = this.type;

            IList<MailContentDetailsEntity> listMailContentDetailsEntity =

            _mailContentDetailsDao.Search(_mailContentDetailsEntity);


            MailContentDetailsEntity _entity = new MailContentDetailsEntity();
            _entity.ID = -1;
            _entity.Name = "";

            //
            this.comboBox_Names.Items.Clear();
            this.comboBox_Names.Items.Add(_entity);
            foreach (var mailContentDetailsEntity in listMailContentDetailsEntity)
            {

                this.comboBox_Names.Items.Add(mailContentDetailsEntity);
            }

            this.comboBox_Names.DisplayMember = "Name";
            this.comboBox_Names.ValueMember = "Id";

            //
            this.SetContentBox();

            //
            textBox_Name.Text = "";
        }

        private void SetContentBox()
        {
            if (this.type == 0)
                this.textBox_ContentEdit.Text = this.objEmailTempletEntity.ToteContent;
            else if (this.type == 1)
                this.textBox_ContentEdit.Text = this.objEmailTempletEntity.PcContent;
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            if("".Equals(this.textBox_Name.Text))
            {
                MessageBox.Show("请输入分类名字！");
                return;
            }

            //+Create Entity
            var _mailContentDetailsEntity = new MailContentDetailsEntity();
            _mailContentDetailsEntity.Content = this.textBox_ContentEdit.Text;
            _mailContentDetailsEntity.EmailContentKeyID = this.objEmailTempletEntity.ContentKeyID;
            _mailContentDetailsEntity.Name = this.textBox_Name.Text;
            _mailContentDetailsEntity.Type = this.type;
            _mailContentDetailsEntity.UpdateUser = ClsCommon.gStrOperatorName;
            _mailContentDetailsEntity.UpdateTime = DateTime.Now;
            //-Create Entity

            _mailContentDetailsDao.SaveOrUpdate(_mailContentDetailsEntity);
            this.Init();
            this.comboBox_Names.Text = _mailContentDetailsEntity.Name;

        }

        private void btn_BaseContent_Click(object sender, EventArgs e)
        {
            this.Init();
        }

        private void btn_Modify_Click(object sender, EventArgs e)
        {
            if ("".Equals(this.textBox_Name.Text))
            {
                MessageBox.Show("请输入分类名字！");
                return;
            }

            var entity = this.getItem();
            if (entity != null)
            {
                entity.Content = textBox_ContentEdit.Text;
                entity.Name = this.textBox_Name.Text;
                entity.UpdateUser = ClsCommon.gStrOperatorName;
                entity.UpdateTime = DateTime.Now;

                _mailContentDetailsDao.SaveOrUpdate(entity);
                //this.Init();
            }else
            {
                MessageBox.Show("未选择修改项!");
            }
        }

        private void btn_Delete_Click(object sender, EventArgs e)
        {
            if (this.comboBox_Names.SelectedItem == null)
            {
                MessageBox.Show("请选择删除项目！");
                return; 
            }

            if(MessageBox.Show( "确认删除么？","",MessageBoxButtons.YesNo) == DialogResult.No)
                return;

            _mailContentDetailsDao.Remove(this.getItem());

            this.comboBox_Names.Text = "";
        }

        private void comboBox_Names_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(this.comboBox_Names.SelectedIndex != 0)
            {
                this.textBox_Name.Text = this.getItem().Name;
                this.textBox_ContentEdit.Text = getItem().Content;
            }
            else
            {
                this.Init();
            }
        }

        private MailContentDetailsEntity getItem()
        {
            return this.comboBox_Names.SelectedItem as MailContentDetailsEntity;
        }
    }
}
